package cn.woochen.mvvmframe.config

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.databinding.ViewDataBinding
import cn.woochen.comlib.BaseViewModel
import cn.woochen.mvvmframe.R
import cn.woochen.mvvmframe.widgets.TitleView

abstract class BaseTitleActivity<DB : ViewDataBinding, VM : BaseViewModel> : BaseActivity<DB, VM>() {

    protected val titleView: TitleView by lazy {
        findViewById(R.id.titleView)
    }

    private val flMainContent: FrameLayout by lazy {
        findViewById(R.id.fl_main_content)
    }

    override fun initData() {
        val contentParent: ViewGroup = findViewById(android.R.id.content)
        val childIndex = 0
        val oldContent: View = contentParent.getChildAt(childIndex)
        contentParent.removeView(oldContent)
        val contentLayout = LinearLayout(this)
        contentLayout.orientation = LinearLayout.VERTICAL
        LayoutInflater.from(this).inflate(R.layout.activity_base_title, contentLayout, true)
        contentParent.addView(contentLayout)
        titleView.setTitle(getTitleName())
        flMainContent.addView(oldContent)
        initContent()
    }

    abstract fun getTitleName(): String

    abstract fun initContent()

}